package com.dengbw.systemtransfer.service;

import com.dengbw.systemtransfer.global.Column;
import com.dengbw.systemtransfer.mapper.SysOrgMapper;
import com.dengbw.systemtransfer.mapper.SysUserMapper;
import com.dengbw.systemtransfer.mapper.SysUserRoleMapper;
import com.dengbw.systemtransfer.model.FuncId;
import com.dengbw.systemtransfer.model.db.SysOrg;
import com.dengbw.systemtransfer.model.db.SysUser;
import com.dengbw.systemtransfer.model.db.SysUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author dengbw
 * @date 2021/11/11 2:52 下午
 */
@Service
public class SysUserService {
    SysUserMapper sysUserMapper;
    SysUserRoleMapper sysUserRoleMapper;
    SysOrgMapper sysOrgMapper;

    @Autowired
    public SysUserService(SysUserMapper sysUserMapper, SysUserRoleMapper sysUserRoleMapper, SysOrgMapper sysOrgMapper) {
        this.sysUserMapper = sysUserMapper;
        this.sysUserRoleMapper = sysUserRoleMapper;
        this.sysOrgMapper = sysOrgMapper;
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveUserRole(String zhenCorpId, Long cunSubId, String cunCorpId) {
        List<SysUser> users = sysUserMapper.queryList(v -> v.eq(Column.SUB_ID, cunSubId));
        sysUserRoleMapper.delete(v -> v.eq(Column.TENANT_ID, cunCorpId));
        users.forEach(u -> {
            SysUserRole role = new SysUserRole();
            role.setTenantId(zhenCorpId);
            role.setSubTenantId(cunSubId);
            role.setUserId(u.getUserId());
            role.setRoleId(u.getUserType().longValue());
            sysUserRoleMapper.insert(role);
        });
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateUser(Long cunSunId) {
        List<FuncId> nhFunc = sysUserMapper.findNonghu(cunSunId);
        nhFunc.forEach(n -> sysUserMapper.update(v -> v.set(Column.NH_FUNCTION, n.getFunid()).eq(Column.USER_ID, n.getUId())));
        List<FuncId> fwFunc = sysUserMapper.findFuwu(cunSunId);
        fwFunc.forEach(f -> sysUserMapper.update(v -> v.set(Column.GB_FUNCTION, f.getFunid()).eq(Column.USER_ID, f.getUId())));
    }

    @Transactional(rollbackFor = Exception.class)
    public void fixMenu(String zhenCorpId) {
        sysUserRoleMapper.fixMenu(zhenCorpId);
    }

    @Transactional(rollbackFor = Exception.class)
    public Integer updateUser(List<Long> subIds) {
        AtomicReference<Integer> count = new AtomicReference<>(0);
        if (CollectionUtils.isEmpty(subIds)) {
            return count.get();
        }
        subIds.forEach(s -> {
            Map<String, Long> orgIdMap = new HashMap<>();
            List<SysOrg> orgs = sysOrgMapper.queryList(v -> v.eq(Column.SUB_ID, s));
            orgs.forEach(o -> orgIdMap.put(o.getOrgName(), o.getOrgGuid()));
            List<SysUser> users = sysUserMapper.queryList(v -> v.eq(Column.SUB_ID, s));
            users.stream()
                    .filter(u -> orgIdMap.get(u.getOrgName()) != null)
                    .forEach(u -> sysUserMapper.update(v -> {
                                v.set("org_guid", orgIdMap.get(u.getOrgName())).eq(Column.USER_ID, u.getUserId());
                                count.getAndSet(count.get() + 1);
                            }
                    ));
        });
        return count.get();
    }
}
